package com.kzy.demoadmin.web.controller;

import com.github.pagehelper.PageInfo;
import com.kzy.demoadmin.web.entity.Book;
import com.kzy.demoadmin.web.entity.Result;
import com.kzy.demoadmin.web.entity.Reserve;
import com.kzy.demoadmin.web.service.BookService;
import com.kzy.demoadmin.web.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 借阅记录
 */
@RestController
@RequestMapping("/reserves")
public class ReserveController {
    
    @Autowired
    private ReserveService reserveService;


    /**
     * 查询所有借阅记录
     * @return
     */
    @GetMapping( "/list")
    public Result findAll( Reserve reserve) {
        PageInfo<Reserve> reserves = reserveService.findAll(reserve);
        return Result.success(reserves);
    }
    
    /**
     * 根据ID查询借阅记录
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Reserve reserve = reserveService.findById(id);
        return Result.success(reserve);
    }
    
    /**
     * 新增借阅记录
     * @param book
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Book book) {
        reserveService.add(book);
        return Result.success();
    }
    
    /**
     * 更新借阅记录
     * @param reserve
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Reserve reserve) {
        reserveService.update(reserve);
        return Result.success();
    }
    
    /**
     * 删除借阅记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        int result = reserveService.deleteById(id);
        return Result.success(result);
    }
    
    /**
     * 根据用户ID查询借阅记录
     * @param userId
     * @return
     */
    @GetMapping("/user/{userId}")
    public Result findByUserId(@PathVariable Integer userId) {
        List<Reserve> reserves = reserveService.findByUserId(userId);
        return Result.success(reserves);
    }
    
    /**
     * 根据书籍ID查询借阅记录
     * @param bookId
     * @return
     */
    @GetMapping("/book/{bookId}")
    public Result findByBookId(@PathVariable Integer bookId) {
        List<Reserve> reserves = reserveService.findByBookId(bookId);
        return Result.success(reserves);
    }
}